<!--  -->
<template>
    <view>
        <u--textarea v-model="details" placeholder="请输入内容" count autoHeight></u--textarea>

        <button @click="jiexi">解析</button>
        <view v-if="show">
            <view style="padding: 30rpx;text-align: center;">
                {{ url }}

            </view>


            <view class="page-section page-section-gap" style="text-align: center;">
                <video class="video" id="demoVideo" :src="j.data.videoUrl">
                </video>
                <button @click="d(j.data.videoUrl)">下载</button>

            </view>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            show: false,
            details: '',
            url: '',
            j: {}
        };
    },

    components: {},

    computed: {},

    mounted() { },

    created() { },

    methods: {
        extractLinksFromDetails() {
            const regex = /(http|https):\/\/[^\s]+/g;
            const links = this.details.match(regex);
            return links;
        },
        jiexi() {
            uni.showLoading({
                title: '解析中'
            });
            this.url = this.extractLinksFromDetails();
            uni.request({
                url: 'https://zhangteng.cloud:5001/api/mainControllers/GotoWatermark?url=' + encodeURIComponent(this.url),

                success: (res) => {
                    console.log('jiexi', JSON.parse(res.data.Result))
                    this.j = JSON.parse(res.data.Result);
                    this.show = true;
                    setTimeout(function () {
                        uni.hideLoading();
                    }, 1000);
                }
            })
        },
        d(url) {
            uni.showLoading({
                title: '下载中'
            });
            uni.downloadFile({
                url: url, //仅为示例，并非真实的资源
                success: (res) => {
                    if (res.statusCode === 200) {
                        console.log('downloadFile', res)
                        console.log('下载成功');
                        let filePath = res.tempFilePath
                        uni.saveVideoToPhotosAlbum({
                            filePath,
                            success: (res) => {
                                uni.showToast({
                                    title: '下载成功',
                                    icon: 'success',
                                })
                                setTimeout(function () {
                                    uni.hideLoading();
                                }, 1000);
                                let fileMgr = wx.getFileSystemManager();
                                // 删除本地文件
                                fileMgr.unlink({
                                    filePath: wx.env.USER_DATA_PATH + '/' + fileName + '.mp4',
                                    success: function (r) {
                                        console.log('unlink-getFileSystemManager')
                                        console.log(r)
                                    },
                                })
                            },
                            complete(res) {
                                console.log('saveVideoToPhotosAlbum-complete')
                                console.log(res)
                            }
                        })
                    }
                }
            });
        },
        // 下载附件
        // 下载视频
        downloadVideo(url) {
            // 自定义 文件名称
            let fileName = new Date().valueOf();
            const task = uni.downloadFile({
                url: url,
                filePath: wx.env.USER_DATA_PATH + '/' + fileName + '.mp4',
                //  拼接本地文件路径
                success: (res) => {
                    let filePath = res.filePath
                    uni.saveVideoToPhotosAlbum({
                        filePath,
                        success: (res) => {
                            uni.showToast({
                                title: '下载成功',
                                icon: 'success',
                            })
                            let fileMgr = wx.getFileSystemManager();
                            // 删除本地文件
                            fileMgr.unlink({
                                filePath: wx.env.USER_DATA_PATH + '/' + fileName + '.mp4',
                                success: function (r) {
                                    console.log('unlink-getFileSystemManager')
                                    console.log(r)
                                },
                            })
                        },
                        complete(res) {
                            console.log('saveVideoToPhotosAlbum-complete')
                            console.log(res)
                        }
                    })
                },
                complete(res) {
                    console.log('downloadFiledownloadFiledownloadFiledownloadFiledownloadFile')
                    console.log(res)
                    //uni.hideLoading()
                }
            })
            task.onProgressUpdate(this.onProgress)
        },




    }
}

</script>
<style lang='scss' scoped></style>